/*
* 用户注册页面
* */
import 'dart:io';

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:flutter_ahj_app/app/regist/bean/message_code.dart';
import 'package:flutter_ahj_app/app/regist/viewmodel/RegistViewModel.dart';
import 'package:flutter_ahj_app/base/basewidget/view/BaseWidget.dart';
import 'package:flutter_ahj_app/base/utils/ToastUtils.dart';
import 'package:flutter_ahj_app/base/utils/mTopBar.dart';
import 'package:flutter_ahj_app/base/utils/screen_util.dart';
import 'package:flutter_ahj_app/base/values/SQColor.dart';
import 'package:flutter_ahj_app/base/values/dimens.dart';
import 'package:flutter/services.dart';
class RegistViewPage extends BaseStatefulWidget {
  final String phone;

  RegistViewPage({this.phone});

  @override
  State<StatefulWidget> initState() {
    // TODO: implement initState
    return RegistPageState();
  }
}

class RegistPageState extends BaseState<RegistViewModel, RegistViewPage> {
  TextEditingController _nameContro = new TextEditingController(text: ''); //用户名
  TextEditingController _userPasswordContro =
      new TextEditingController(text: ''); //用户密码
  TextEditingController _userSurePasswordContro =
      new TextEditingController(text: ''); //用户确认密码
  TextEditingController _userNiceNameContro =
      new TextEditingController(text: ''); //用户昵称
  TextEditingController _userDengjiContro =
      new TextEditingController(text: ''); //用户等级
  TextEditingController _userGongShiContro =
      new TextEditingController(text: ''); //用户工时

  File imageFile; //选择的用户头像
  String isRequest = "0";
  String userNme, userErrorName; //用户名
  String userPassword, userErrorPassword; //用户密码
  String userSurePassword, userErrorSurePassword; //用户确认密码
  String userNiceName, userErrorNiceName; //用户昵称
  String userDengji, userErrorDengji; //用户等级
  String userGongShi, userErrorGongShi; //用户工时

  @override
  initState() {
    super.initState();
    setState(() {
      _nameContro.text = widget.phone;
    });
  }

  @override
  Widget initWidget() {
    // TODO: implement initWidget
    return new Scaffold(
      appBar: MTopBar(
              title: "用户注册", rightButtonText: "确认", onRightButtonClick: _sure)
          .create(),
      body: new SingleChildScrollView(
        child: new ConstrainedBox(
          constraints: new BoxConstraints(
            minHeight: 200.0,
          ),
          child: new Stack(
            children: <Widget>[
              new Container(
                padding: EdgeInsets.fromLTRB(8, 0, 12, 0),
                child: new Column(
                  children: <Widget>[
                    new Row(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: <Widget>[
                        new GestureDetector(
                          child: imageFile == null
                              ? Image.asset(
                                  "img/iv_head_none.png",
                                  width: Dimens.dimens64,
                                  height: Dimens.dimens64,
                                )
                              : Image.file(
                                  imageFile,
                                  width: Dimens.dimens64,
                                  height: Dimens.dimens64,
                                ),
                          onTap: () {
                            _pickImage();
                          },
                        ),
                      ],
                    ),
                    new Container(
                        margin: EdgeInsets.fromLTRB(16, 16, 16, 16),
                        child: TextField(
                          style: TextStyle(
                              color: SQColor.per87Black,
                              fontSize: Dimens.textSize32),
                          controller: _nameContro,
                          keyboardType: TextInputType.text,
                          decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(Dimens.dimens16),
                            labelText: "用户名*",
                            errorText: userErrorName,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                  Radius.circular(Dimens.dimens4)),
                            ),
                          ),
                          onChanged: (String str) {
                            setState(() {
                              userNme = _nameContro.text;
                              (userNme == null || userNme == "")
                                  ? userErrorName = "请输入用户名"
                                  : userErrorName = null;
                            });
                          },
                        )),
                    new Container(
                        margin: EdgeInsets.fromLTRB(8, 16, 16, 8),
                        child: TextField(
                          inputFormatters: [
                            // WhitelistingTextInputFormatter(RegExp("[0-9]|[a-z]|[A-Z]")),
                          ],
                          style: TextStyle(
                              color: SQColor.per87Black,
                              fontSize: Dimens.textSize32),
                          controller: _userPasswordContro,
                          keyboardType: TextInputType.text,
                          decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(Dimens.dimens16),
                            labelText: "输入密码*",
                            errorText: userErrorPassword,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                  Radius.circular(Dimens.dimens4)),
                            ),
                          ),
                          onChanged: (String str) {
                            setState(() {
                              userPassword = _userPasswordContro.text;
                              (userPassword == null || userPassword == "")
                                  ? userErrorPassword = "请输入用户密码"
                                  : userErrorPassword = null;
                            });
                          },
                        )),
                    new Container(
                        margin: EdgeInsets.fromLTRB(8, 16, 16, 8),
                        child: TextField(
                          inputFormatters: [
                            // WhitelistingTextInputFormatter(RegExp("[0-9]|[a-z]|[A-Z]")),
                          ],

                          style: TextStyle(
                              color: SQColor.per87Black,
                              fontSize: Dimens.textSize32),
                          controller: _userSurePasswordContro,
                          keyboardType: TextInputType.text,
                          decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(Dimens.dimens16),
                            labelText: "确认密码*",
                            errorText: userErrorSurePassword,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                  Radius.circular(Dimens.dimens4)),
                            ),

                          ),
                          onChanged: (String str) {
                            setState(() {
                              userSurePassword = _userSurePasswordContro.text;
                              (userSurePassword == null ||
                                      userSurePassword == "")
                                  ? userErrorSurePassword = "请输入用户名"
                                  : (userSurePassword != userPassword)
                                      ? userErrorSurePassword = "两次密码不一致"
                                      : userErrorSurePassword = null;
                            });
                          },

                        )),
                    new Container(
                        margin: EdgeInsets.fromLTRB(8, 16, 16, 8),
                        child: TextField(
                          style: TextStyle(
                              color: SQColor.per87Black,
                              fontSize: Dimens.textSize32),
                          controller: _userNiceNameContro,
                          keyboardType: TextInputType.text,
                          decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(Dimens.dimens16),
                            labelText: "用户昵称*",
                            errorText: userErrorNiceName,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                  Radius.circular(Dimens.dimens4)),
                            ),
                          ),
                          onChanged: (String str) {
                            setState(() {
                              userNiceName = _userNiceNameContro.text;
                              (userNiceName == null || userNiceName == "")
                                  ? userErrorNiceName = "请输入用户昵称"
                                  : userErrorNiceName = null;
                            });
                          },
                        )),
                    new Container(
                        margin: EdgeInsets.fromLTRB(8, 16, 16, 8),
                        child: TextField(
                          style: TextStyle(
                              color: SQColor.per87Black,
                              fontSize: Dimens.textSize32),
                          controller: _userDengjiContro,
                          keyboardType: TextInputType.text,
                          decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(Dimens.dimens16),
                            labelText: "用户等级*",
                            errorText: userErrorDengji,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                  Radius.circular(Dimens.dimens4)),
                            ),
                          ),
                          onChanged: (String str) {
                            setState(() {
                              userDengji = _userDengjiContro.text;
                              (userDengji == null || userDengji == "")
                                  ? userErrorDengji = "请输入用户等级"
                                  : userErrorDengji = null;
                            });
                          },
                        )),
                    new Container(
                        margin: EdgeInsets.fromLTRB(8, 16, 16, 8),
                        child: TextField(
                          style: TextStyle(
                              color: SQColor.per87Black,
                              fontSize: Dimens.textSize32),
                          controller: _userGongShiContro,
                          keyboardType: TextInputType.text,
                          decoration: InputDecoration(
                            contentPadding: EdgeInsets.all(Dimens.dimens16),
                            labelText: "用户工时费*",
                            errorText: userErrorGongShi,
                            border: OutlineInputBorder(
                              borderRadius: BorderRadius.all(
                                  Radius.circular(Dimens.dimens4)),
                            ),
                          ),
                          onChanged: (String str) {
                            setState(() {
                              userGongShi = _userGongShiContro.text;
                              (userGongShi == null || userGongShi == "")
                                  ? userErrorGongShi = "请输入用户名"
                                  : userErrorGongShi = null;
                            });
                          },
                        ))
                  ],
                ),
              ),
              isRequest == "1"
                  ? new Container(
                      width: ScreenUtil.getScreenW(context),
                      height: ScreenUtil.getScreenH(context),
                      child: Center(
                        child: CircularProgressIndicator(),
                      ),
                    )
                  : new Container()
            ],
          ),
        ),
      ),
    );
  }

  String name;

  var encryptedString;

  _sure() async {
    if (isRequest == "1") {
      return;
    }
    bool isregest = true;
    if (isEmpty(userNme)) {
      userErrorName = "请输入用户名";
      isregest = false;
    } else if (isEmpty(userNiceName)) {
      userErrorNiceName = "请输入用户昵称";
      isregest = false;
    } else if (isEmpty(userDengji)) {
      isregest = false;
      userErrorDengji = "请输入用户等级";
    } else if (isEmpty(userGongShi)) {
      isregest = false;
      userErrorGongShi = "请输入用户工时";
    } else if (isEmpty(userPassword)) {
      isregest = false;
      userErrorPassword = "请输入用户密码";
    } else if (isEmpty(userSurePassword)) {
      isregest = false;
      userErrorSurePassword = "请输入用户确认密码";
    } else if (userSurePassword != userPassword) {
      isregest = false;
      userErrorSurePassword = "两次输入密码不一致";
    }

    if (isregest) {
      isRequest = "1";
      viewModel.setRegistData(userNme, userPassword, "", "", "", "", "", "");
      Stream<MessageCode> sss = viewModel.getDataStream();

      sss.listen((messageData) => {stateData(messageData)});
    }
    setState(() {});
  }

  _pickImage() async {
    showDialog<bool>(
      context: context,
      builder: (BuildContext context) {
        return new SimpleDialog(
          title: new Text('选择附件视图'),
          children: <Widget>[
            new SimpleDialogOption(
              child: new InkWell(
                child: new Container(
                  child: new Text(
                    '照相',
                    style: new TextStyle(fontSize: 16),
                  ),
                  height: 48,
                  alignment: Alignment.centerLeft,
                ),
              ),
              onPressed: () {
                Navigator.of(context).pop(true);
              },
            ),
            new SimpleDialogOption(
              child: new InkWell(
                child: new Container(
                  child: new Text(
                    '从相册选择',
                    style: new TextStyle(fontSize: 16),
                  ),
                  height: 48,
                  alignment: Alignment.centerLeft,
                ),
              ),
              onPressed: () {
                Navigator.of(context).pop(true);
              },
            ),
          ],
        );
      },
    );
  }

  stateData(MessageCode errorData) {
    if (errorData != null) {
      ToastUtils.showToast(errorData.msg);
      if (errorData.code == 0) {
        //注册成功
        Navigator.pop(context);
      } else {
        isRequest = "0";
        setState(() {});
      }
    } else {
      ToastUtils.showToast("注册失败");
      isRequest = "0";
      setState(() {});
    }
  }
}
